package cn.stylefeng.guns.cloud.workflow.modular.controller;

import cn.stylefeng.guns.cloud.libs.scanner.annotation.PostResource;
import cn.stylefeng.guns.cloud.libs.scanner.stereotype.ApiResource;
import cn.stylefeng.guns.cloud.model.page.PageResult;
import cn.stylefeng.guns.cloud.model.web.response.ResponseData;
import cn.stylefeng.guns.cloud.workflow.modular.model.params.ApproveParam;
import cn.stylefeng.guns.cloud.workflow.modular.model.params.TaskParam;
import cn.stylefeng.guns.cloud.workflow.modular.model.result.TodoTaskResult;
import cn.stylefeng.guns.cloud.workflow.modular.service.CommonTaskService;
import cn.stylefeng.guns.cloud.workflow.modular.service.DoneTaskService;
import cn.stylefeng.guns.cloud.workflow.modular.service.TodoTaskService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 任务控制器
 *
 * @Author xuyuxiang
 * @Date 2019/11/14 19:45
 **/
@RestController
@ApiResource(name = "任务管理接口", path = "/task")
@Api(tags = "任务管理接口")
public class TaskController {

    @Autowired
    private CommonTaskService commonTaskService;

    @Autowired
    private TodoTaskService todoTaskService;

    @Autowired
    private DoneTaskService doneTaskService;

    /**
     * 获取任务详情
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:51
     **/
    @PostResource(name = "获取任务详情", path = "/viewTaskDetail")
    @ApiOperation(value = "获取任务详情")
    public ResponseData viewTaskDetail(@RequestParam("processInstanceId") String processInstanceId) {
        Map<String, Object> resultMap = commonTaskService.viewTaskDetail(processInstanceId);
        return ResponseData.success(resultMap);
    }

    /**
     * 获取候选人列表
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:48
     **/
    @PostResource(name = "获取候选人列表", path = "/candidateList")
    @ApiOperation(value = "获取候选人列表")
    public ResponseData candidateList(@RequestParam("processInstanceId") String processInstanceId) {
        PageResult pageResult = commonTaskService.candidateList(processInstanceId);
        return ResponseData.success(pageResult);
    }

    /**
     * 获取待办任务列表
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:49
     **/
    @PostResource(name = "获取待办任务列表", path = "/todoList")
    @ApiOperation(value = "获取待办任务列表", response = TodoTaskResult.class)
    public ResponseData todoList(@RequestBody TaskParam taskParam) {
        PageResult pageResult = todoTaskService.list(taskParam);
        return ResponseData.success(pageResult);
    }

    /**
     * 获取已办任务列表
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:50
     **/
    @PostResource(name = "获取已办任务列表", path = "/doneList")
    @ApiOperation(value = "获取已办任务列表", response = TodoTaskResult.class)
    public ResponseData doneList(@RequestBody TaskParam taskParam) {
        PageResult pageResult = doneTaskService.list(taskParam);
        return ResponseData.success(pageResult);
    }

    /**
     * 审批
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:52
     **/
    @PostResource(name = "审批任务", path = "/doneTask")
    @ApiOperation(value = "审批任务")
    public ResponseData doneTask(@RequestBody ApproveParam approveParam) {
        todoTaskService.doneTask(approveParam);
        return ResponseData.success();
    }

    /**
     * 委托
     *
     * @Author xuyuxiang
     * @Date 2019/11/14 19:54
     **/
    @PostResource(name = "委托任务", path = "/entrust")
    @ApiOperation(value = "委托任务")
    public ResponseData entrust(@RequestBody TaskParam taskParam) {
        todoTaskService.entrust(taskParam);
        return ResponseData.success();
    }
}
